package com.gexiaobao.pigeon.ui.adapter

import android.annotation.SuppressLint
import android.graphics.Typeface
import androidx.appcompat.widget.AppCompatTextView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.model.bean.PigeonBean
import com.gexiaobao.pigeon.app.util.RxToast

/**
 * @author:hxw
 * @date  :2023/1/3
 * @desc  :
 */
@SuppressLint("NotifyDataSetChanged")
class AddTrainingFlyAdapter(data: ArrayList<PigeonBean.RingInfo>, gone: Boolean) :
    BaseQuickAdapter<PigeonBean.RingInfo, BaseViewHolder>(R.layout.item_add_training, data) {

    private var mOnItemClickListener: OnItemClickListener? = null
    private val checkList = ArrayList<PigeonBean.RingInfo>()
    private lateinit var tvDetail: AppCompatTextView
    private val maxSelection = 5 // 最大选择数量
    private var isGone = false

    init {
        isGone = gone
    }

    override fun convert(holder: BaseViewHolder, item: PigeonBean.RingInfo) {
        val tvRing = holder.getView<AppCompatTextView>(R.id.tv_item_add_training_ring_id)
        if (isGone) {
            tvRing.setTypeface(null, Typeface.BOLD)
        }
        holder.setText(R.id.tv_item_add_training_ring_id, item.ringId)
        tvDetail = holder.getView(R.id.train_fly_add_item_detail)
        holder.setGone(R.id.train_fly_add_item_detail, isGone)
        val fruit = data[getItemPosition(item)]
        if (fruit.isChecked) {
            holder.setImageResource(R.id.iv_item_add_training_checked, R.mipmap.checked)
        } else {
            holder.setImageResource(R.id.iv_item_add_training_checked, R.mipmap.unchecked)
        }

        holder.itemView.setOnClickListener {
            if (data[getItemPosition(item)].isChecked) {
                fruit.isChecked = false
                notifyItemChanged(getItemPosition(item))
            } else {
                if (isGone) {
                    if (checkList.size < maxSelection) {
                        fruit.isChecked = true
                        notifyItemChanged(getItemPosition(item))
                    } else {
                        RxToast.warning("最多只能选择 5 项")
                        return@setOnClickListener
                    }
                } else {
                    fruit.isChecked = true
                    notifyItemChanged(getItemPosition(item))
                }
            }
            checkList.clear()
            data.forEach {
                if (it.isChecked) {
                    checkList.add(it)
                } else {
                    checkList.remove(it)
                }
            }
        }
        mOnItemClickListener?.onItemClick(getItemPosition(item), checkList)

    }

    /**
     * 全选方法
     *
     * @return List<Fruit> 返回返选后的集合
     */
    fun setInvertSelection(): List<PigeonBean.RingInfo> {
//        val newList = ArrayList<PigeonBean.RingInfo>()
        data.forEach {
            if (!it.isChecked) {
                checkList.add(it)
            }
        }
        data.forEach {
            it.isChecked = true
        }
        notifyDataSetChanged()
        return checkList
    }

    /**
     * 清除所有所选
     */
    fun setClear() {
        data.forEach {
            if (it.isChecked) {
                it.isChecked = false
            }
        }
        notifyDataSetChanged()
    }

    fun setOnMultipleChoiceClickListener(listener: OnItemClickListener?) {
        mOnItemClickListener = listener
    }

    interface OnItemClickListener {
        fun onItemClick(position: Int, list: List<PigeonBean.RingInfo>)
    }
}